package edu.pet.Servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import edu.pet.entity.*;
import java.util.*;
import edu.pet.DAO.PetDAO;

public class PetUpdateServlet extends HttpServlet {

	public PetUpdateServlet() {
		super();
	}

	public void destroy() {
		super.destroy(); 
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String handle = request.getParameter("handle");//add update show(showAdd showUpdate types owners)
		System.out.println(handle);
		
		ArrayList<Type> types = new ArrayList<Type>();
		ArrayList<Owner> owners = new ArrayList<Owner>();
		PetDAO dao = new PetDAO();
		HttpSession session = request.getSession();
		
		if(handle.equals("showAdd")){
			types = dao.getType();
			owners = dao.getOwner();
			session.setAttribute("types", types);
			session.setAttribute("owners", owners);
			response.sendRedirect("../user/addpet.jsp");
		}
		
		if(handle.equals("showUpdate")){
			types = dao.getType();
			owners = dao.getOwner();
			session.setAttribute("types", types);
			session.setAttribute("owners", owners);
			response.sendRedirect("../user/petupdate.jsp");
		}
		if(handle.equals("add")){
			int owner_id = Integer.parseInt(request.getParameter("owner_name"));
			String pet_name = request.getParameter("pet_name");
			int type_id = Integer.parseInt(request.getParameter("type"));
			String birthday = request.getParameter("birthday");
			System.out.println(owner_id + pet_name + type_id + birthday);
			Pet pet = new Pet();
			pet.setName(pet_name);
			pet.setOwner_id(owner_id);
			pet.setType_id(type_id);
			pet.setBirthday(birthday);
			if(dao.addPet(pet))
				session.setAttribute("msg", "宠物添加成功！");
			else
				session.setAttribute("msg", "宠物添加失败！");
			response.sendRedirect("../user/petsearch.jsp");
		}
		if(handle.equals("update")){
			int pet_id = Integer.parseInt(request.getParameter("pet_id"));
			String pet_name = request.getParameter("pet_name");
			String birthday = request.getParameter("birthday");
			int owner_id = Integer.parseInt(request.getParameter("owner_id"));
			int type_id = Integer.parseInt(request.getParameter("type_id"));
			//System.out.println(pet_id+pet_name+birthday+owner_id+type_id);
			Pet pet = new Pet();
			pet.setPet_id(pet_id);
			pet.setName(pet_name);
			pet.setBirthday(birthday);
			pet.setType_id(type_id);
			pet.setOwner_id(owner_id);
			if(dao.updatePet(pet))
				session.setAttribute("msg", "修改成功");
			else
				session.setAttribute("msg", "修改失败");
			response.sendRedirect("../user/petsearch.jsp");
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doGet(request, response);
	}

	public void init() throws ServletException {
		
	}

}
